package com.woniuxy.his.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.his.common.Result;
import com.woniuxy.his.controller.form.AddCheckSortForm;
import com.woniuxy.his.entity.Check;
import com.woniuxy.his.mapper.CheckDetailMapper;
import com.woniuxy.his.mapper.CheckMapper;
import com.woniuxy.his.service.impl.CheckServiceImpl;
import com.woniuxy.his.vo.AccountProVo;
import com.woniuxy.his.vo.AccountProjectVo;
import com.woniuxy.his.vo.CheckResultVo;
import com.woniuxy.his.vo.InspectVo;
import com.woniuxy.his.controller.form.SetStatusForm;
import com.woniuxy.his.service.ICheckService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 检查表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@RestController
//@RequestMapping("/his/check")
@CrossOrigin("*")
@Slf4j
public class CheckController {

    //检查表查询缴费状态,和患者名称
    @Resource
    private ICheckService checkService;

    @Resource
    private RedissonClient redissonClient;


    /**
     * 前端需要病人的姓名和检查项目，需要用缴费时间为条件生成排序，项目检查表，项目详情表
     * @return
     */
    // 项目检查即加载待检查患者并排队
    @GetMapping("/findInspect")
    public Result findInspect(
            @RequestParam(name = "pageSize",defaultValue = "3") int pageSize
            ,@RequestParam(name = "current",defaultValue = "1") int current){
        log.info("前端传入参数：页容量{}，当前页{}",pageSize,current);
        //查询检查表删选已缴费患者
        IPage allIspaids = checkService.findAllIspaid(pageSize,current);

        log.info("Controller层查询所有缴费患者信息：{}",allIspaids);
        return Result.success("40000","查询成功",allIspaids);
    }

    //修改检查项目付费情况并添加序列号
    @PostMapping("checkPay")
    public Result checkPay(@RequestBody SetStatusForm setStatusForm){
        log.info("获取的检查项目id:{}",setStatusForm);
        RBucket<Integer> bucket = redissonClient.getBucket("sortNumber");
        if (!bucket.isExists()) {
            bucket.set(1);
        }else{
            Integer sortNumber = bucket.get();
            sortNumber++;
            bucket.set(sortNumber);
        }
        Integer sortNum = bucket.get();
        Integer num = checkService.checkPay(sortNum, setStatusForm.getCheckId());
        if (num==0) {
            return Result.fail("40000","修改失败",null);
        }
        return Result.success("20000","修改成功",null);
    }


    //项目划价后,传递项目信息,写入检查表和检查详情表
    @PostMapping("/accountProject")
    public Result accountProject(@RequestBody AccountProjectVo accountProjectVo){
        log.info("tableCheck={},totalProject={},guaId={},doctorId={}",accountProjectVo.getTableCheck(),accountProjectVo.getTotalProject(),accountProjectVo.getGuaId(),accountProjectVo.getDoctorId());

        Integer i = checkService.accountProject(accountProjectVo);
        return i>0?Result.success("1111","",i):Result.fail("0000","失败",i);
    }

    //获取检查结果,返回前端页面
    @GetMapping("/queryCheckResult")
    public Result queryCheckResult(Integer guaId){
        log.info("guaId={}",guaId);

        ArrayList<CheckResultVo> checkResultVos = checkService.queryCheckResult(guaId);
        return Result.success("1111","找到了",checkResultVos);
    }


    /**
     * 根据检查详情表id修改详情表的记录修改时间字段，然后重新排队到队伍的最末尾
     * @param addCheckSortForm 传入的检查详情表id
     * @return
     */
    @PostMapping("/checkTheNumber")
    public Result updateCheckDetailUpdateTimeByCheckDetailId(@RequestBody AddCheckSortForm addCheckSortForm){
        log.info("传入的检查详情表ID**:{}",addCheckSortForm.getCheckId());

        boolean isCheck = checkService.updateCheckDetailUpdateTimeByCheckDetailId(addCheckSortForm.getCheckId());

        return isCheck ? Result.success("20000","成功", null) : Result.fail("40002", "系统繁忙", null);
    }
}

